package com.cennavi.service.impl;

import com.cennavi.function.CostFunction;
import com.cennavi.function.KpiFunction;
import com.cennavi.function.ListSumFunction;
import com.cennavi.function.MinFunction;
import com.cennavi.mapper.*;
import com.cennavi.service.CommonService;
import com.googlecode.aviator.AviatorEvaluator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by lixiaoshuang on 2018/10/16.
 */
@Service
public class CommonServiceImpl implements CommonService {
    @Resource
    WeightMapper weightMapper;
    @Resource
    QualityMapper qualityMapper;
    @Resource
    CommonMapper commonMapper;
    @Resource
    FormulaMapper formulaMapper;
    @Resource
    CostMapper costMapper;

    @Override
    public Object getColumnsToShow(String tabkeName) {
        List<String> tabkeList = new ArrayList<String>();
        if (tabkeName!=null && !tabkeName.trim().equals("")){
            tabkeList = Arrays.asList(tabkeName.trim().split(","));
        }
        return commonMapper.queryColumnsToShow(tabkeList);



    }

    @Override
    public Object getScore(List<Map> list1) {
        AviatorEvaluator.addFunction(new ListSumFunction());
        AviatorEvaluator.addFunction(new KpiFunction());
        List<Map> weightlist=weightMapper.queryData(new ArrayList());
        Map weight=weightlist.get(0);
        Map map=formulaMapper.selectByName("sum");
        Map<String,Object> env=new HashMap<>();
//        cost数据
        Map costMap=costMapper.selectByCaseType("1");
        //封装的公里的集合
        List<Double> list = new ArrayList<Double>();
        list.add(10.0);
        list.add(20.0);
        list.add(20.34);
        list.add(45.76);
        List cost=new ArrayList<>();
        for(Double item:list){
            Map temp=new HashMap<>();
            temp.putAll(costMap);
            costMap.put("km",item);
            Object result = AviatorEvaluator.execute("(km-startmileage)*kmprice+startprice", costMap);
            cost.add(result);
        }
        costMap.put("list",cost);

        costMap.put("s1",8);
        costMap.put("p1",100);
        AviatorEvaluator.addFunction(new MinFunction());
        AviatorEvaluator.addFunction(new CostFunction());
        Object result = AviatorEvaluator.execute("cost(((s1-startmileage)*kmprice+p1),min(list))", costMap);
        weight.put("cost",result);
        Double quality=this.getQuality(null);
        weight.put("quality",quality);
        weight.put("eta",30);
        weight.put("car",100);
        return  AviatorEvaluator.execute(map.get("text").toString(),weight);




    }
    private double getQuality(Map map1){

        Object obj=this.getColumnsToShow("quality");
        List<String> columns=(List<String>)obj;
        Map<String,Object> env1=new HashMap<>();
        String fm="";
        List<String> columnsWeight=new ArrayList<>();
        for(String item:columns){

            if(item.equals("month") || item.equals("year")){
                continue;
            }else {
                if(fm.length()==0){
                    fm=item+"*"+item+"_wt";
                }else {
                    fm=fm+"+"+item+"*"+item+"_wt";
                }
                columnsWeight.add(item+"_wt");
                List<Map> list=qualityMapper.queryData(item);
                List doubleList=new ArrayList<>();
                for(Map i:list){
                    doubleList.add(Double.parseDouble(i.get("data").toString()));
                }
                Map<String, Object> env = new HashMap<String, Object>();
                env.put("list", doubleList);
                env.put("a", 0);
                env.put("b", 2);
                env.put("c",2);
                env.put("d",4);
                env.put("wt",0.2);
                env.put("size",list.size());
                Object result3 = AviatorEvaluator.execute("kpi(((wt*listSum(list,a,b)))/size,(((1-wt)*listSum(list,c,d)))/size)", env);
//                env1.put(item,(new BigDecimal(result3.toString()).setScale(4,BigDecimal.ROUND_HALF_UP)).doubleValue());
                env1.put(item,result3);
            }
        }
        List<Map> maplist=weightMapper.queryData(columnsWeight);
        Map<String,Object> map=maplist.get(0);
        for(String s:map.keySet()){
            env1.put(s,Double.parseDouble(map.get(s).toString()));
//            env1.put(s,1);
        }
//        map.putAll(env1);
        System.out.println("公式为："+fm);
        Object result4=AviatorEvaluator.execute(fm,env1);
        System.out.println("结果为："+result4);
        return Double.parseDouble(result4.toString());
    }


    public List<String> getTableNames(){
       return commonMapper.queryListTables();
    }
}
